package com.example.pei.ithinklearnt.view.activity;

import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.pei.ithinklearnt.R;
import com.example.pei.ithinklearnt.adapter.NewFriendAdapter;
import com.example.pei.ithinklearnt.adapter.base.OnRecyclerViewListener;
import com.example.pei.ithinklearnt.bean.UserInfo;
import com.example.pei.ithinklearnt.db.NewFriend;
import com.example.pei.ithinklearnt.db.NewFriendManager;
import com.example.pei.ithinklearnt.model.UserModel;
import com.example.pei.ithinklearnt.view.base.BaseActivity;
import com.example.pei.ithinklearnt.view.base.IMutlipleItem;
import com.example.pei.ithinklearnt.view.customview.EmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.example.pei.ithinklearnt.Config.STATUS_VERIFIED;

public class NewFriendActivity extends BaseActivity {

    @BindView(R.id.ll_root)
    ConstraintLayout ll_root;
    @BindView(R.id.swipe_target)
    EmptyRecyclerView rc_view;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout sw_refresh;
    @BindView(R.id.tv_empty)
    TextView textView;

    NewFriendAdapter adapter;
    LinearLayoutManager layoutManager;

    @OnClick(R.id.button_backward)
    void back()
    {
        finish();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_friend);

        //单一布局
        IMutlipleItem<NewFriend> mutlipleItem = new IMutlipleItem<NewFriend>() {

            @Override
            public int getItemViewType(int position, NewFriend c) {
                return 0;
            }

            @Override
            public int getItemLayoutId(int viewtype) {
                return R.layout.item_new_friend;
            }

            @Override
            public int getItemCount(List<NewFriend> list) {
                return list.size();
            }
        };
        adapter = new NewFriendAdapter(this,mutlipleItem,null);
        rc_view.setAdapter(adapter);
        layoutManager = new LinearLayoutManager(this);
        rc_view.setLayoutManager(layoutManager);

        rc_view.setEmptyView(textView);
        //批量更新未读未认证的消息为已读状态
        NewFriendManager.getInstance(this).updateBatchStatusReaded();

        setListener();
    }

    private void setListener(){
        ll_root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ll_root.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                query();
                sw_refresh.finishRefresh(true);
            }
        });
        sw_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                query();
                sw_refresh.finishRefresh(true);
            }
        });
        adapter.setOnRecyclerViewListener(new OnRecyclerViewListener() {
            @Override
            public void onItemClick(View v, int position) {
                //toast("点击"+position);
            }

            @Override
            public boolean onItemLongClick(View v,int position) {
                toast("删除了"+position);
                NewFriendManager.getInstance(NewFriendActivity.this).deleteNewFriend(adapter.getItem(position));
                adapter.remove(position);
                return true;
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        query();
        sw_refresh.finishRefresh(true);
    }

    /**
     查询本地会话
     */
    public void query(){

        List<NewFriend> friends=NewFriendManager.getInstance(this).getAllNewFriend();
        List<UserInfo> friendList=UserModel.getInstance().getLocalFreinds(this);

        if(friendList!=null){
            //如果本地已经有这个好友了，那么状态应该设置成 STATUS_VERIFIED
            for(UserInfo info:friendList){
                for(int i=0;i<friends.size();i++){
                    if(friends.get(i).getName().equals(info.getUsername())){
                        friends.get(i).setStatus(STATUS_VERIFIED);
                    }
                }
            }
        }
        //由于一个人可能会发送多次好友请求，这里使用set保证一个用户只有一条请求记录
        if(friends.size()>=1) {
            List<NewFriend> set = new ArrayList<>();
            set.add(friends.get(0));
            for (NewFriend f : friends) {
                for (int i = 0; i < set.size(); i++) {
                    if (f.getName().equals(set.get(i).getName())) {
                        //重复了 跳出循环
                        break;
                    }
                    if (i == set.size()-1) {
                        //遍历到最后也没有找到一样的，则添加
                        set.add(f);
                    }
                }
            }
        adapter.bindDatas(set);
        }
        adapter.notifyDataSetChanged();
    }

}
